package com.hfmall.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hfmall.entity.HfCategory;
import com.hfmall.mapper.HfCategoryMapper;
import com.hfmall.service.IHfCategoryService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author liclo
 * @since 2021-08-12
 */
@Slf4j
@Service
public class HfCategoryServiceImpl extends ServiceImpl<HfCategoryMapper, HfCategory> implements IHfCategoryService {

    @Resource
    private HfCategoryMapper hfCategoryMapper;
    @Resource
    private HfCategoryServiceImpl hfCategoryService;

    /**
     * 分页查询所有一级分类信息
     *
     * @return Page
     * @author liclo
     * @date 2021/8/13
     */
    @Override
    public Page getFLevel(Integer current) {
        if (current == null) {
            current = 1;
        }
        Page p = new Page(current, 10, false);
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("Category_id", 0);

        return hfCategoryMapper.selectPage(p, queryWrapper);
    }

    /**
     * 分页查询所有次级分类信息
     *
     * @author liclo
     * @date 2021/8/13
     */
    @Override
    public Page getSLevel(Integer current, Integer pid) {
        if (current == null) {
            current = 1;
        }
        Page p = new Page(current, 10, false);
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("Category_id", pid);

        return hfCategoryMapper.selectPage(p, queryWrapper);
    }

    /**
     * 分页查询所有上级分类信息
     *
     * @author liclo
     * @date 2021/8/13
     */
    @Override
    public Page getLastLevel(Integer current, String level, Integer pid) {
        if (current == null) {
            current = 1;
        }
        Page p = new Page(current, 10, false);
        QueryWrapper queryWrapper = new QueryWrapper();

        // 第一级展示所有商品，第二级只展示对应pid商品
        if ("2".equals(level)) {
            queryWrapper.eq("level", "1");
        } else {

            QueryWrapper q = new QueryWrapper();
            q.eq("id", pid);
            HfCategory hfCategory = hfCategoryMapper.selectOne(q);

            queryWrapper.eq("Category_id", hfCategory.getCategoryId());
        }
        return hfCategoryMapper.selectPage(p, queryWrapper);

    }

    /**
     * 按树形结构返回所有分类
     *
     * @author liclo
     * @date 2021/8/13
     */
    @Override
    public List<HfCategory> tree() {
        return hfCategoryMapper.tree();
    }

    @Override//查询所有; 查询分级类别从lv开始(lv,lv+1,lv+2)三级;参数lv-结果获取lv开始的三级分类
    public List<HfCategory> getCategoryByLv(int lv) {
        List<HfCategory> hfCategorys = hfCategoryMapper.getCategoryByLv(lv);
        hfCategorys.forEach(c -> {
            List<HfCategory> chr = hfCategoryMapper.getTreeCategoryByPid(c.getId());
            c.setChildren(chr);
        });
        return hfCategorys;
    }

    /**
     * 参数id
     * 查询类别-参数id-结果获取其树型-子分类
     *
     * @param id
     */
    @Override
    public HfCategory getCategoryById(int id) {
        HfCategory hfCategory = hfCategoryMapper.selectById(id);
        if ("1".equals(hfCategory.getLevel())) { //本身为1级 查2,3级
            List<HfCategory> HfCategorys = hfCategoryMapper.getTreeCategoryByPid(hfCategory.getId());
            hfCategory.setChildren(HfCategorys);
        } else if ("2".equals(hfCategory.getLevel())) {//本身为2级 查3级
            List<HfCategory> HfCategorys = hfCategoryMapper.getCategoryByPid(hfCategory.getId());
            hfCategory.setChildren(HfCategorys);
        }
        return hfCategory;
    }

    /**
     * 根据父id查询类别
     *
     * @param Category_id
     * @return
     */
    @Override
    public List<HfCategory> getCategoryByPid(int Category_id) {
        return hfCategoryMapper.getCategoryByPid(Category_id);
    }

    /**
     * 类别id查询父类别
     *
     * @param id @return HfCategory
     */
    @Override
    public HfCategory getHfCategoryLv1(int id) {
        HfCategory hfCategory = hfCategoryMapper.selectById(id);
        if ("1".equals(hfCategory.getLevel())) { //一级
            return hfCategory;
        } else {//2,3级
            HfCategory hfCategoryNext = hfCategoryMapper.selectById(hfCategory.getCategoryId()); //上一级分类
            if ("1".equals(hfCategoryNext.getLevel())) {//2级的一级类别
                return hfCategoryNext;
            } else {//3级的一级类别
                HfCategory hfCategoryEnd = hfCategoryMapper.selectById(hfCategoryNext.getCategoryId());
                return hfCategoryEnd;
            }
        }
    }

    /**
     * 按树形结构返回所有三级分类
     *
     * @author liclo
     * @date 2021/8/18
     */
    @Override
    public List<HfCategory> getAll() {
        return hfCategoryMapper.getAll();
    }

    /**
     * 根据父id查询其所有Lv3类别
     *
     * @param id@return lv3 HfCategory
     */
    @Override
    public List<HfCategory> getCategoryLv3Byid(int id) {
        HfCategory hfCategory = hfCategoryService.getCategoryById(id); //查询当前id是几级

//        log.debug(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>.此id所有lv3类别:{}",hfCategory);
        List<HfCategory> hfCategoryLv3 = new ArrayList<>();//对应分类的所有三级分类
        if(hfCategory!=null) {
            if (hfCategory.getChildren() == null) {
                hfCategoryLv3.add(hfCategory);
            } else {
                hfCategory.getChildren().forEach(c -> {
                    if (c.getChildren()!=null) {
                        c.getChildren().forEach(c2 -> {
                            hfCategoryLv3.add(c2);
                        });
                    } else {
                        hfCategoryLv3.add(c);
                    }
                });

            }
        }
//        log.debug(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>.此id所有lv3类别:{}",hfCategoryLv3);
        return hfCategoryLv3;
    }


}
